package com.neusoft.yuejian_6.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.neusoft.yuejian_6.R;
import com.neusoft.yuejian_6.javabean.Train;
import com.neusoft.yuejian_6.javabean.Trainn;
import com.neusoft.yuejian_6.javabean.User;
import com.neusoft.yuejian_6.model.TrainModel;
import com.neusoft.yuejian_6.model.UserModel;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class EnterTrainActivity extends AppCompatActivity {

    private ImageView back;
    private Button enter_button;
    private int user_id;
    private int train_id;
    private TextView train_name,train_price,train_type,user_name,user_phone;
    // private ImageView  addbutton,reducebutton;
    // private int num;
    // private Double sum;
    // private Double tprice;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_enter_train);
        train_id=getIntent().getIntExtra("train_id",-1);
        initViews();//初始化
        initEvents();//点击事件处理

    }

    private void initViews() {
        train_name = (TextView) findViewById(R.id.train_name);

        user_name = (TextView) findViewById(R.id.user_name);
        user_phone = (TextView) findViewById(R.id.user_phone);
        train_price = (TextView) findViewById(R.id.train_price);

        train_type = (TextView) findViewById(R.id.train_type);
        enter_button = (Button) findViewById(R.id.enter_button);

        back= (ImageView) findViewById(R.id.back);
    }

    private void initEvents() {
        //返回上一页
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        Bundle bundle = this.getIntent().getExtras();
      train_id = bundle.getInt("train_id");
        SharedPreferences sp = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        user_id = sp.getInt("user_id", 0);

        UserModel userModel = new UserModel();
        Call callu = userModel.getUserInfo(user_id);
        callu.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                User user = response.body();
                user_name.setText(user.getUser_name());
                user_phone.setText(user.getUser_phone());
            }
            @Override
            public void onFailure(Call<User> call, Throwable t) {
            }
        });






        TrainModel trainModel = new TrainModel();
        Call<Train> call = trainModel.gettraindetail(train_id);
        call.enqueue(new Callback<Train>() {
            @Override
            public void onResponse(Call<Train> call, Response<Train> response) {
                Train data = (Train) response.body();  //获取数据
                String trainname = data.getTrain_name();
                int tprice = data.getTrain_price();
                int ttype=data.getTrain_type();
                train_name.setText(trainname);
                train_price.setText("￥"+ tprice+"");
                if(ttype==0)
                {
                    train_type.setText("入门");
                }
                else if(ttype==1){
                    train_type.setText("菜鸟");
                }
                else if(ttype==2){
                    train_type.setText("进阶");
                }

                //  t_type.setText(ttype+"");

            }

            @Override
            public void onFailure(Call<Train> call, Throwable t) {

            }
        });



        enter_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TrainModel trainModel = new TrainModel();
                Call<Trainn> call = trainModel.addtrainn(user_id,train_id);
                call.enqueue(new Callback<Trainn>() {
                    @Override
                    public void onResponse(Call<Trainn> call, Response<Trainn> response) {
                        Trainn trainn=response.body();
                        if (trainn.getSuccess().equals("0")) {
                            Toast.makeText(EnterTrainActivity.this, "失败", Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(EnterTrainActivity.this, "报名成功", Toast.LENGTH_LONG).show();
//                            Intent intent=new Intent(EnterTrainActivity.this,BuyActivity.class);
//                            startActivity(intent);
                        }
                    }
                    @Override
                    public void onFailure(Call<Trainn> call, Throwable t) {

                    }
                });
            }
        });


    }
}

